package com.hwadee.hello.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.getRequestDispatcher("/WEB-INF/views/student/login.jsp").forward(req, resp);;
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String user = req.getParameter("user");
		String password = req.getParameter("password");
		if("admin".equals(user) && "123456".equals(password)) {
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
			
			ServletContext application = getServletContext();
			application .setAttribute("application", user);
			resp.sendRedirect("/hello/students");
		}else {
			req.setAttribute("msg", "账号或密码错误！");
			
			req.getRequestDispatcher("/WEB-INF/views/student/login.jsp").forward(req, resp);;
		}
	}

	
}
